/*
* @file    :drv.c
* @module  :
* @author  :LingFengLu
* @date    :2023-06-06
*/

#include "drv.h"
#include "rng_manager.h"
#include "rf_driver_ll_system.h"
#include "rf_driver_ll_utils.h"

//Global variable definition    全局变量定义


//Private variable definition    私有变量定义
static uint32 m_u32_ResetSource = 0;
static uint8 m_u8_WorkState = DRV_WORK_STATE_FINISH;



//Private function declaration   私有函数申明
static void Drv_GetResetSource(void);
static void Drv_SystemInit(void);







//Public function definition   公共函数定义
void Drv_Init(void)
{
	Drv_SystemInit();
	DrvGpio_Init() ;
	DrvUart_Init(DRV_UART_DEVICE_ID_3) ;
}

uint Drv_SetConfig
(
uint ui_Parameter,
uint8 *u8p_Value,
uint ui_Length
)
{
	switch (ui_Parameter)
	{
	    case DRV_PARAM_WORK_STATE:
		m_u8_WorkState = *(u8p_Value);
		break;		
	    default:
		return FUNCTION_FAIL;
	}
	
	return FUNCTION_OK;
}

uint Drv_GetConfig
(
uint ui_Parameter,
uint8 *u8p_Value,
uint *uip_Length
)
{
	switch (ui_Parameter)
	{
	    case DRV_PARAM_WORK_STATE:
		*(u8p_Value) = m_u8_WorkState;
		break;		
	    case DRV_PARAM_RESET_FLAG:
		*((uint32 *)u8p_Value) = m_u32_ResetSource;
		break;
	    default:
		return FUNCTION_FAIL;
	}
	return FUNCTION_OK;
}

void Drv_SystemPorcessAction(void)
{
	HAL_VTIMER_Tick();
	BLE_STACK_Tick();
	NVMDB_Tick();
	Ble_AppProcess();
	DrvTimer_process() ;
	if (OTA_Tick() == 1)
	{
		OTA_Jump_To_New_Application();
	}
}

void Drv_Sleep(void)
{
	PowerSaveLevels stopLevel;
	WakeupSourceConfig_TypeDef wakeupIO;
	wakeupIO.IO_Mask_High_polarity = WAKEUP_PA10; 
	wakeupIO.IO_Mask_Low_polarity = 0;
	wakeupIO.RTC_enable = 0;
	wakeupIO.LPU_enable = 0;
	if(m_u8_WorkState == DRV_WORK_STATE_FINISH)
	{
		/* Power Save Request */ 
		HAL_PWR_MNGR_Request(POWER_SAVE_LEVEL_STOP_NOTIMER, wakeupIO, &stopLevel);
	}
}

uint32 Drv_Random(void)
{
	uint32 u32_Value = 0;
	
	
	RNGMGR_GetRandom32(&u32_Value) ;
	return u32_Value;
}


void Drv_DelayMs
(
uint16 u16_Delay
)
{
	LL_mDelay(u16_Delay);
}

void Drv_SoftRest(void)
{
	NVIC_SystemReset();
}







//Private function definition   私有函数定义
static void Drv_SystemInit(void)
{
	SystemInit(SYSCLK_64M, BLE_SYSCLK_32M) ;
	LL_Init1msTick(SystemCoreClock);
}

static void Drv_GetResetSource(void)
{
	
}












#ifdef EN_TEST
#define EN_POWER_PIN          GPIO_CHANNEL(PORT_C,PIN4)
#define ADC_EN_TEMP_PIN       GPIO_CHANNEL(PORT_B,PIN0)
#define ADC_TEMP_PIN          GPIO_CHANNEL(PORT_B,PIN1)
#define TEST_INPUT_PIN        GPIO_CHANNEL(PORT_A,PIN6)

static void callback(void)
{
	static uint8 u8_Flag = 0;
	uint32 u32_Value = 0;
	// uint8 u8_Buff[]="Hello world\n";
	// LOG_INFO("callback");
	
	// DrvUart_SendData(u8_Buff,sizeof(u8_Buff));
	if(u8_Flag)
	{
		u8_Flag = 0;
		u32_Value = DrvAdc_Single(0,ADC_EN_TEMP_PIN);
		LOG_INFO("ADC_EN_TEMP_PIN adc value:%d",u32_Value);
		LOG_INFO("v value:%d",u32_Value*2420/0xFFF);
	}
	else
	{
		u8_Flag = 1;
		u32_Value = DrvAdc_Single(1,ADC_TEMP_PIN);
		LOG_INFO("ADC_TEMP_PIN adc value:%d",u32_Value);
		LOG_INFO("v value:%d",u32_Value*1210/0xFFF);
	}
	
	// LOG_INFO("State:%d", DrvGpio_InGet(TEST_INPUT_PIN));
	
}

// static void callback(void)
// {
//     static uint32 u32_Count = 0;

//     LOG_INFO("Hello~~~~~");
//     u32_Count ++;
//     if(u32_Count<= 20)
//     {
//         LOG_INFO("stop");
//         uint8 u8_Value = 1;
//         DrvTimer_SetConfig(DRV_TIMER_ID0,DRV_TIMER_PARAM_SWITCH,
//                 (uint8 *)&u8_Value,
//                 sizeof(u8_Value));  
//     }

// }

static void Drv_Test(void)
{
	// callback();
	//    uint8 u8_Value = 1;
	// uint i;
	// for(i = 0; i< 10; i++)
	// {
	//     Drv_DelayMs(1000);
	//     LOG_INFO("Test Delay");
	// }
	// LOG_INFO("Test Delay ok");
	
	// DrvGpio_Set(EN_POWER_PIN,
	//             MODE_OUTPUT,
	//             1);
	
	
	// DrvTimer_SetConfig(DRV_TIMER_PARAM_CALLBACK,
	//                     (uint8 *)callback,
	//                     sizeof(fp_timer_callback));
	// DrvTimer_SetConfig(DRV_TIMER_PARAM_START,
	//                     &u8_Value,
	//                     sizeof(u8_Value));
	
	
	// DrvGpio_Set(TEST_INPUT_PIN,
	//             MODE_INPUT_PULL_DOWN,
	//             0);
	
	
	// uint32 u32_Period;
	// bool b_Repeat;
	// uint8 u8_Value; 
	// u32_Period = 1000;
	// b_Repeat = true;
	// u8_Value = 1;
	// DrvTimer_SetConfig(DRV_TIMER_ID0,DRV_TIMER_PARAM_PERIOD,
	//                 (uint8 *)&u32_Period,
	//                 sizeof(u32_Period));           
	// DrvTimer_SetConfig(DRV_TIMER_ID0,DRV_TIMER_PARAM_REPEAT,
	//                 (uint8 *)&b_Repeat,
	//                 sizeof(b_Repeat));
	// DrvTimer_SetConfig(DRV_TIMER_ID0,DRV_TIMER_PARAM_CALLBACK,
	//                 (uint8 *)callback,
	//                 sizeof(fp_timer_callback));   
	// DrvTimer_SetConfig(DRV_TIMER_ID0,DRV_TIMER_PARAM_SWITCH,
	//                 (uint8 *)&u8_Value,
	//                 sizeof(u8_Value));  
	
	// uint8 u8_Buffer[5]={1,2,3,4,5};
	//  uint8 u8_Buffer[5]={0};
	// uint ui_Result =   DrvFlash_FileLoad( 10,u8_Buffer,5);
	// LOG_INFO(" File Load ui_Result:%d",ui_Result);
	// for(uint i = 0; i<5; i++)
	// {
	//     LOG_INFO("Data:%d",u8_Buffer[i]);
	// }
	
	
	
}
#endif
